﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TrainingsApp.Logic;
using TrainingsApp.WebModel.Storage;
using System.Web.SessionState;

namespace TrainingsApp.IPADApp.Ajax {
    /// <summary>
    /// Summary description for AjaxGet
    /// </summary>
    public class AjaxGet : IHttpHandler, IRequiresSessionState {

        public void ProcessRequest(HttpContext context) {
            int cmd = ZJFramework10.Utility.QueryStringManager.GetValue<int>("cmd");
            switch (cmd) {
                case 2000:
                    ValidRegisterUsername();
                    break;
                case 2005:
                    ValidForgetUsernameAndEmail();
                    break;
                case 3000:///GetAreaByProvince
                    GetAreaByProvince();
                    break;
                case 3001:///GetRegionByRegion
                    GetRegionByRegion();
                    break;
                case 3002:///GetSchool
                    GetSchool();
                    break;
                case 3003:///GetClass
                    GetClass();
                    break;
                case 3004:///GetGrade
                    GetGrade();
                    break;
                case 3005:///GetJiaoCai
                    GetJiaoCai();
                    break;
                case 5000:///LeftTree
                    SetLeftTree();
                    break;
            }

            context.Response.End();
            return;
        }

        private void SetLeftTree() {
            int state = ZJFramework10.Utility.QueryStringManager.GetValue<int>("state");
            UserCookieFacade.SetLeftTreeState(state);
            ResponseText("OK");
        }

        private void ValidRegisterUsername() {
            string queryName = ZJFramework10.Utility.QueryStringManager.GetValue<string>("rn", "rn");
            string userName = ZJFramework10.Utility.QueryStringManager.GetValue<string>(queryName);

            if (!string.IsNullOrEmpty(userName)) {
                if (TrainingsApp.Logic.TS_User_Logic.TS_User_Exists(userName.Trim())) {
                    ResponseText("200");
                }
                else {
                    ResponseText("404");
                }
            }
            else {
                ResponseText("500");
            }

        }


        private void ValidForgetUsernameAndEmail() {
            string queryName = ZJFramework10.Utility.QueryStringManager.GetValue<string>("rn", "rn");
            string queryEName = ZJFramework10.Utility.QueryStringManager.GetValue<string>("en", "en");
            string userName = ZJFramework10.Utility.QueryStringManager.GetValue<string>(queryName, string.Empty);
            string email = ZJFramework10.Utility.QueryStringManager.GetValue<string>(queryEName, string.Empty);

            if (!string.IsNullOrEmpty(userName)) {
                if (TrainingsApp.Logic.TS_User_Logic.TS_User_ExistsEmailAndUserName(userName.Trim(), email.Trim())) {
                    ResponseText("200");
                }
                else {
                    ResponseText("404");
                }
            }
            else {
                ResponseText("404");
            }

        }

        private void GetAreaByProvince() {
            int provinceId = ZJFramework10.Utility.QueryStringManager.GetValue<int>("p", 0);
            var listSource = TrainingsApp.Logic.TS_Area_Logic.TS_Area_ListByProvince(provinceId);
            List<DropItem> dataSource = new List<DropItem>();
            dataSource.Add(new DropItem { Text = "市", Value = -1 });
            if (listSource == null || listSource.Count <= 0) {
                ResponseJson(dataSource);
            }
            else {
                dataSource.AddRange(listSource.Select(u => new DropItem {
                    Text = u.AreaName,
                    Value = u.Idx.Value
                }));
                ResponseJson(dataSource);
            }
        }

        private void GetRegionByRegion() {
            int areId = ZJFramework10.Utility.QueryStringManager.GetValue<int>("a", 0);
            var listSource = TrainingsApp.Logic.TS_Region_Logic.TS_Region_ListByArea(areId);
            List<DropItem> dataSource = new List<DropItem>();
            dataSource.Add(new DropItem { Text = "区/县", Value = -1 });
            if (listSource == null || listSource.Count <= 0) {
                ResponseJson(dataSource);
            }
            else {
                dataSource.AddRange(listSource.Select(u => new DropItem {
                    Text = u.RegionName,
                    Value = u.Idx.Value
                }));
                ResponseJson(dataSource);
            }
        }

        private void GetSchool() {
            int areaId = ZJFramework10.Utility.QueryStringManager.GetValue<int>("a", 0);
            int provinceId = ZJFramework10.Utility.QueryStringManager.GetValue<int>("p", 0);
            int regionId = ZJFramework10.Utility.QueryStringManager.GetValue<int>("r", 0);

            var listSource = TrainingsApp.Logic.TS_School_Logic.TS_School_ListByWhere(provinceId, areaId, regionId);
            List<DropItem> dataSource = new List<DropItem>(50);
            dataSource.Add(new DropItem { Text = "学校名称", Value = -1 });
            if (listSource == null || listSource.Count <= 0) {
                ResponseJson(dataSource);
            }
            else {
                dataSource.AddRange(listSource.Select(u => new DropItem { Text = u.SchoolName, Value = u.Idx.Value }));
                ResponseJson(dataSource);
            }

        }

        private void GetClass() {
            int schoolId = ZJFramework10.Utility.QueryStringManager.GetValue<int>("s", 0);
            int grade = ZJFramework10.Utility.QueryStringManager.GetValue<int>("g", 0);
            var listSource = TrainingsApp.Logic.TS_Class_Logic.TS_Class_ListClass(schoolId, grade);
            List<DropItem> dataSource = new List<DropItem>(10);
            dataSource.Add(new DropItem { Text = "班级名称", Value = -1 });
            if (dataSource != null && dataSource.Count > 0) {
                dataSource.AddRange(listSource.Select(u => new DropItem {
                    Text = u.ClassName,
                    Value = u.Idx.Value
                }));
            }
            ResponseJson(dataSource);
        }

        private void GetGrade() {
            int schoolId = ZJFramework10.Utility.QueryStringManager.GetValue<int>("s", 0);
            var listSource = TrainingsApp.Logic.TS_Class_Logic.TS_Class_ListGrade(schoolId);
            List<DropItem> dataSource = new List<DropItem>(10);
            dataSource.Add(new DropItem { Text = "年级", Value = -1 });
            if (dataSource != null && dataSource.Count > 0) {
                dataSource.AddRange(listSource.Select(u => new DropItem {
                    Text = u.GradeName,
                    Value = u.Idx
                }));
            }
            ResponseJson(dataSource);
        }


        private void GetJiaoCai() {
            int courseId = ZJFramework10.Utility.QueryStringManager.GetValue<int>("c", 0);
            var listSource = TrainingsApp.Logic.TS_TeachMaterials_Logic.TS_TeachMaterials_ListByCourse(courseId);
            List<DropItem> dataSource = new List<DropItem>();
            dataSource.Add(new DropItem { Text = "教材", Value = -1 });
            if (dataSource != null && dataSource.Count > 0) {
                dataSource.AddRange(listSource.Select(u => new DropItem {
                    Text = u.Name,
                    Value = u.Idx.Value
                }));
            }
            ResponseJson(dataSource);
        }


        public void ResponseJson(object json) {
            HttpContext.Current.Response.ContentType = "text/json";
            HttpContext.Current.Response.Write(ZJFramework10.Serialize.Helper.SerializeString(json));
        }

        public void ResponseText(string text) {
            HttpContext.Current.Response.ContentType = "text/json";
            HttpContext.Current.Response.Write(text);
        }

        public bool IsReusable {
            get {
                return false;
            }
        }
    }
}